package com.bdqn.controller;

import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysUserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

@Controller
public class ExampleController {
    @Resource
    private SysUserService userService;

    @GetMapping(value = "/hello/{id}")
    public String getUser(@PathVariable("id") Long usrId , Model model){
        SysUser user = userService.getUser(usrId);
        model.addAttribute("user" , user);
        model.addAttribute("userName", "url");
        return "demo/hello";
    }

    @GetMapping(value = "/inline")
    public String inline(Model model){
        model.addAttribute("userName", "url");
        return "demo/inline";
    }

    @GetMapping(value = "/string")
    public String getString(Model model , HttpServletRequest request){
        model.addAttribute("userName" , "AKT");
        request.setAttribute("test" , "request");
        request.getSession().setAttribute("test" , "sess");
        model.addAttribute("date" , new Date());
        return "demo/string";
    }

    @GetMapping(value = "/if")
    public String ifunless(Model model){
        model.addAttribute("flag" , "yes");
        return "demo/if";
    }

    @GetMapping(value = "/list")
    public String list (Model model){
        List<SysUser>list = userService.findAllUsers();
        model.addAttribute("users" , list);
        return "demo/list";
    }
    @GetMapping(value = "/fragment")
    public String layout(){
        return "demo/fragment";
    }
}
